/* Copyright (c) 2004-2023 Peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.service.processor.user.edit;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.core.enums.Symbol;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.core.util.PasswordUtils;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.manage.core.domain.Organization;
import info.peigen.hotpot.manage.core.domain.Position;
import info.peigen.hotpot.manage.core.domain.Role;
import info.peigen.hotpot.manage.core.domain.User;
import info.peigen.hotpot.manage.core.service.order.UserEditOrder;
import info.peigen.hotpot.manage.core.service.processor.AbstractHotpotManageServiceProcessor;
import org.noear.solon.annotation.Component;

import java.util.List;

/**
 * <b>(UserEditBusinessProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024/6/12
 */
@SuppressWarnings("rawtypes")
@Component
public class UserEditBusinessProcessor extends AbstractHotpotManageServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 25;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        UserEditOrder order = (UserEditOrder) serviceContext.getOrder();

        User domain = (User) serviceContext.getDomain();
        // 修改密码
        domain.setPassword(PasswordUtils.generatePwdEncrypt(order.getNewPassword(), domain.getSalt(), domain.getCreateTime()));

        // 更新组织
        updateOrg(domain, order.getOrganizationIds());
        // 更新职位
        updatePos(domain, order.getPositionIds());
        // 更新角色
        updateRole(domain, order.getRoleIds());
    }

    private void updateOrg(User domain, String orgIds) {
        if (StrUtil.isEmpty(orgIds)) {
            return;
        }
        List<Organization> organizations = organizationRepository.active(StrUtil.split(orgIds, Symbol.COMMA.code()));
        domain.setOrganizations(organizations);
    }

    private void updatePos(User domain, String posIds) {
        if (StrUtil.isEmpty(posIds)) {
            return;
        }
        List<Position> positions = positionRepository.active(StrUtil.split(posIds, Symbol.COMMA.code()));
        domain.setPositions(positions);
    }

    private void updateRole(User domain, String roleIds) {
        if (StrUtil.isEmpty(roleIds)) {
            return;
        }
        List<Role> roles = roleRepository.active(StrUtil.split(roleIds, Symbol.COMMA.code()));
        domain.setRoles(roles);
    }

    private void updateMobile(User domain) {
        // 登录名必须符合手机号或邮箱规则
        if (StrUtil.isNotEmpty(domain.getLoginName())) {
            // 1. 符合Email规则
            if (Validator.isEmail(domain.getLoginName())) {
                domain.setEmail(domain.getLoginName());
            }

            // 2. 符合Mobile规则
            else if (Validator.isMobile(domain.getLoginName())) {
                domain.setMobile(domain.getLoginName());
            }

            // 3. 都不符合
            else {
                throw new BusinessException(ResultCode.FAILURE, "登录名必须符合手机号格式或Email格式");
            }
        }
    }

}
